﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IP.Core.IPData;
using IP.Core.IPUserService;
using BKIDS.CDBNames;
using IP.Core.IPCommon;
using BKIDS;
using BKIUS;
using System.Data;


public partial class BaoCao_F611_Danh_sach_thanh_vien_theo_level : System.Web.UI.Page
{
    #region Members
    US_V_DM_THANH_VIEN_VI_TRI m_us_v_dm_thanh_vien = new US_V_DM_THANH_VIEN_VI_TRI();
    DS_V_DM_THANH_VIEN_VI_TRI m_ds_v_dm_thanh_vien = new DS_V_DM_THANH_VIEN_VI_TRI();
    #endregion


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_data_2_cbo_vitri_congviec();
            load_data_2_grid();
        }
    }
    #region Private Methods
    // Load dữ liệu lên lưới.
    private void load_data_2_grid()
    {
        m_us_v_dm_thanh_vien.fill_dataset_by_vi_tri_cong_viec(m_ds_v_dm_thanh_vien, CIPConvert.ToDecimal(m_cbo_vi_tri_cong_viec.SelectedValue));
        m_grv_thanh_vien.DataSource = m_ds_v_dm_thanh_vien.V_DM_THANH_VIEN_VI_TRI;
        m_grv_thanh_vien.DataBind();
        if (m_ds_v_dm_thanh_vien.V_DM_THANH_VIEN_VI_TRI == null || m_ds_v_dm_thanh_vien.V_DM_THANH_VIEN_VI_TRI.Count == 0)
            m_lbl_thong_bao.Text = "Không có thành viên thuộc vị trí "+ m_cbo_vi_tri_cong_viec.SelectedItem;
        else m_lbl_thong_bao.Text = "";
    }

    private void load_data_2_cbo_vitri_congviec()
    {
        US_CM_DM_TU_DIEN v_cm_dm_tu_dien = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_cm_dm_tu_dien = new DS_CM_DM_TU_DIEN();
        DataRow v_dr_none = v_ds_cm_dm_tu_dien.CM_DM_TU_DIEN.NewCM_DM_TU_DIENRow();
        v_ds_cm_dm_tu_dien.EnforceConstraints = false;
        v_dr_none[CM_DM_TU_DIEN.ID] = "0";
        v_dr_none[CM_DM_TU_DIEN.MA_TU_DIEN] = "All";
        v_dr_none[CM_DM_TU_DIEN.TEN] = "Tất cả";
        v_ds_cm_dm_tu_dien.CM_DM_TU_DIEN.Rows.InsertAt(v_dr_none, 0);
        //Select * from CM_DM_TU_DIEN WEHERE ID_LOAI_TU_DIEN = 3 và đổ vào trong v_ds_cm_dm_tu_dien
        v_cm_dm_tu_dien.FillDataset(v_ds_cm_dm_tu_dien, "WHERE ID_LOAI_TU_DIEN = " + LOAI_TU_DIEN.VI_TRI_CONG_VIEC);

        m_cbo_vi_tri_cong_viec.DataTextField = CM_DM_TU_DIEN.TEN;
        m_cbo_vi_tri_cong_viec.DataValueField = CM_DM_TU_DIEN.ID;

        m_cbo_vi_tri_cong_viec.DataSource = v_ds_cm_dm_tu_dien.CM_DM_TU_DIEN;
        m_cbo_vi_tri_cong_viec.DataBind();
    }

    private void load_data_2_excel_search(decimal ip_id_vi_tri_cong_viec)
    {
        m_ds_v_dm_thanh_vien.Clear();
        m_us_v_dm_thanh_vien.fill_dataset_by_vi_tri_cong_viec(m_ds_v_dm_thanh_vien, ip_id_vi_tri_cong_viec);
    }
    #endregion
    #region Public Method
    public string mapping_level_vt(decimal ip_id_vi_tri)
    {
        
        US_CM_DM_TU_DIEN v_us_level = new US_CM_DM_TU_DIEN(ip_id_vi_tri);
        if (v_us_level != null)
            return v_us_level.strTEN_NGAN;
        return "";
    }
    #endregion
    #region Export Excel
    private void loadDSExprort(ref string strTable)
    {
        int v_i_so_thu_tu = 0;
        // Mỗi cột dữ liệu ứng với từng dòng là label
        foreach (DataRow grv in this.m_ds_v_dm_thanh_vien.V_DM_THANH_VIEN_VI_TRI.Rows)
        {
            strTable += "\n<tr>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + ++v_i_so_thu_tu + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + CIPConvert.ToStr(grv[V_DM_THANH_VIEN_VI_TRI.TEN_DAY_DU]) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + CIPConvert.ToStr(grv[V_DM_THANH_VIEN_VI_TRI.MA_THANH_VIEN]) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_level_vt(CIPConvert.ToDecimal(grv[V_DM_THANH_VIEN_VI_TRI.ID_LEVEL])) + "</td>";
            
            strTable += "\n</tr>";
        }
    }

    private void loadTieuDe(ref string strTable)
    {
        load_data_2_excel_search(CIPConvert.ToDecimal(m_cbo_vi_tri_cong_viec.SelectedValue));
        strTable += "<table cellpadding='2' cellspacing='0' class='cssTableReport'>";
        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width: 100%;  height: 40px; font-size: large; color:White; background-color:#810C15;' nowrap='wrap'>BKI_PRO - BÁO CÁO DANH SÁCH THÀNH VIÊN PROJECT VỊ TRÍ CÔNG VIỆC" + "</td>";
        strTable += "\n</tr>";
        //
        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width: 100%;  height: 40px; font-size: large; color:White; background-color:#810C15;' nowrap='wrap'>VỊ TRÍ CÔNG VIỆC" + m_cbo_vi_tri_cong_viec.SelectedItem + "</td>";
        strTable += "\n</tr>";
        //
        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n</tr>";
        //
        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>PR_MEMV_" + DateTime.Today.ToString("yyyyMMdd") + "</td>";
        strTable += "\n</tr>";
        strTable += "\n</table>";
        //table noi dung
        strTable += "<table cellpadding='2' cellspacing='0' class='cssTableReport'>";
        strTable += "\n<tr>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>STT</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Họ và tên</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Mã thành viên</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Level Vị trí</td>";        
        strTable += "\n</tr>";
        loadDSExprort(ref strTable);
        strTable += "\n</table>";
    }

    private string loadExport()
    {
        try
        {
            string strHTML = "<html xmlns:o='urn:schemas-microsoft-com:office:office'"
            + "\n xmlns:x='urn:schemas-microsoft-com:office:excel'"
            + "\n xmlns='http://www.w3.org/TR/REC-html40'>"
            + "\n <head>"
            + "\n <meta http-equiv=Content-Type content='text/html; charset=utf-8'>"
            + "\n <meta name=ProgId content=Excel.Sheet>"
            + "\n <meta name=Generator content='Microsoft Excel 11'>"
            + "\n <link rel=File-List href='Book1_files/filelist.xml'>"
            + "\n <style id='Book1_28091_Styles'><!--table"
            + "\n 	{mso-displayed-decimal-separator:'\\.';"
            + "\n 	mso-displayed-thousand-separator:'\\,';}"
            + ".cssTitleReport"
            + "{font-family: tahoma; font-size: 11px;font-weight:normal;border: 1px #000000 solid;text-align:left;}"
            + ".cssTableView"
            + "{color:#FFFFFF;background-color:#006699;font-family: tahoma,Arial,Times New Roman; font-size: 12px;font-weight:bold;border: 1px #000000 solid;}"
            + "\n 	--></style>"
            + "\n 	</head>"
            + "\n 	<body><div id='Book1_28091' align=center x:publishsource='Excel'>";
            string strTable = "";
            loadTieuDe(ref strTable);
            strHTML += strTable;
            strHTML += "\n </div></body> ";
            strHTML += "\n </html> ";

            return strHTML;
        }
        catch
        {
            return "";
        }
    }
    #endregion
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            string html = loadExport();
            string strNamFile = "BaoCaoDSThanhVienTheoViTriCongViec" + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + ".xls";
            Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
            Response.Clear();
            Response.AppendHeader("content-disposition", "attachment;filename=" + strNamFile);
            Response.Charset = "UTF-8";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "text/csv";
            Response.ContentType = "application/vnd.ms-excel";
            this.EnableViewState = false;
            Response.Write("\r\n");
            Response.Write(html);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(v_e);
        }
    }
    protected void m_cbo_vi_tri_cong_viec_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            load_data_2_grid();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(v_e);
        }
    }
}